Productive R Workflow Module 3
1 Lesson 1
1.1 Clean tables
Author discusses two preferred methods for incorporating tables in Quarto documents. Different approaches are discussed in this link.
1.2 Interactive tables with DT
Here is one of my go-to techniques when showcasing Quarto 😍.
With just two extra lines of code, you can unveil an interactive table that allows:
sorting
filtering
searching
pagination
It offers readers the convenience of accessing the data directly within your report. The magic of this functionality rests in the DT library. DT stands for DataTables: the JavaScript library supports it.
1.3 Static tables with kable() from the package knitr
Code
| species | average_bill_length |
|---|---|
| Adelie | 38.79139 |
| Chinstrap | 48.83382 |
| Gentoo | 47.50488 |
2 Lesson 2
Interactive tables
An interactive chart allows the user to perform actions: zooming, hovering a marker to get a tooltip, choosing a variable to display and more.
R offers a set of packages called the html widgets.
They allow to build interactive charts directly from R. 🔥
This course is not about data visualization and I already curated the best packages for interactive dataviz in the R graph gallery.
2.1 Plotly and ggplotly()
Code
ggplotly provides information for each point displayed in the graph. On the following chart you can zoom, hover, pan, export to png and more!
I find plotly so convenient: it’s an instant win without the need to learn anything new!
However, it’s not always the ideal choice. For instance Leaflet is better suited for mapping, and Dygraph excels in handling time series data. I’ve detailed these alternatives in the R Graph Gallery if you’re interested!
Also, remember that not all graphs require interactivity. For example, a basic bar chart remains just as effective in its static form, containing all the necessary information.
3 Lesson 3
3.1 Report header
In Quarto, you have the flexibility to include extensive information in the document header.
While the official documentation offers a comprehensive list of options, it’s important to strike a balance to prevent overwhelming readers.
Here’s an example of a Quarto header that effectively incorporates these elements in the YAML section of the Quarto document :
title: "Exploring the Simpson's Paradox..."
subtitle: "And simultaneously demonstrating ..."
description: "This document is..."
author:
name: "Yan Holtz"
affiliation: "Independant 😀"
email: yan.holtz.data@gmail.com
keywords: "Quarto, Paradox, Data Analysis"
date: today
3.2 Header appearance
With our informative header in place, let’s enhance its visual appeal.
One effective method is to utilize the title-block-banner option to introduce a background color, adding depth to the header. It subtly elevates its presence.
...
title-block-banner: "#f0f3f5"
title-block-banner-color: "black"
...
3.3 Going further
Quarto opens up endless possibilities for customization.
→ The Front Matter and Title blocks sections provide a comprehensive list of out-of-the-box options, covering licenses, copyrights, citations, multiple authors, metadata labels, and more.
→ Later in the course, we’ll explore CSS (Cascading Style Sheets) techniques to customize any element within the report.
→ For advanced users, template partials offer the ability to build the header structure from scratch, although this is a more advanced feature.
4 Lesson 4
4.1 Apply your style with CSS
This course teaches you how to transform your R code into a visually appealing report in HTML format.
Essentially, we’re creating a miniature website 🔥!
It is time to understand what HTML is, and explore how to personalize it using CSS, a stylesheet language.
Hello World!
I am a tiny website
When encountering a tag like h1, it recognizes it as a level 1 title and know how to format it accordingly. Same with the p tag that stands for paragraph, and all other tag types.
4.2 Style it with CSS
CSS is the language we use to style an HTML document.
Basically, we could create a file called style.css and add the following content in it:
h1 {
color: red;
}
This means: whenever you find an h1 tag, make the text red.
CSS is complicated.
It transforms a plain black-and-white text assembly into a visually stunning website 😅.
However, you can leverage it to make minor adjustments to your quarto document with the assistance of Google or ChatGPT!
4.3 Quarto, HTML and CSS
An example of CSS
h1 {
padding-top: 100px;
}
h2, h3 {
padding-top: 84px;
}
a {
color: #69b3a2;
}
→ The first two rules add additional spacing above the headings of levels 1, 2, and 3. I find that allowing the document to have more breathing room enhances readability, making it easier for readers to distinguish between different sections.
→ The final rule modifies the appearance of hyperlinks (a elements), setting their font color to #69b3a2, a color that appeals to my preference.
5 Lesson 5
Create your dataviz theme
Creating a custom theme for ggplot2
5.1 The theme() function
Code
library(tidyverse)
mtcars |>
ggplot(aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Customized Scatterplot", x = "Miles Per Gallon", y = "Weight") +
theme(
plot.title = element_text(color = "#69b3a2", size = 14, face = "bold"),
plot.margin = margin(t = 3, r = 3, b = 3, l = 3, unit = "cm")
)5.2 A set of predefined theme
Customizing a theme can feel overwhelming, with numerous options available to tweak and adjust.
However, a plethora of predefined themes exist, ready to be utilized in a matter of seconds!
Within the ggplot2 library alone, eight distinct themes are readily available, with theme_grey() serving as the default and theme_minimal emerging as a popular alternative
Furthermore, beyond ggplot2’s native themes, numerous external packages offer additional thematic options. Among them, one that I love is the theme_ipsum() theme from the hrbrthemes package:
Code
library(hrbrthemes)
library(tidyverse)
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Customized Scatterplot", x = "Miles Per Gallon", y = "Weight") +
theme_ipsum() +
theme(
plot.title = element_text(color = "#69b3a2", size = 14, face = "bold"),
)5.3 Make your own theme
To elevate your visualizations to the next level of refinement, creating your own theme based on one of the predefined themes is the optimal strategy.
For example, let’s build a theme called yan_holtz_theme which builds upon the ipsum theme:
Code
library(hrbrthemes)
yan_holtz_theme <- function() {
theme_ipsum() +
theme(
plot.title = element_text(color = "#69b3a2", size = 18, face = "bold"),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7)
)
}You need to install or call the library(hrbrthemes) for the yan_holtz_theme() function to work.
This theme changes the title color and makes it pretty big. It also decreases the size of the axis text elements.
Now, let’s apply the theme to a graph:
Code
mtcars |>
ggplot(aes(x = mpg, y = wt)) +
geom_point() +
labs(title = "Customized Scatterplot",
x = "Miles Per Gallon", y = "Weight") +
yan_holtz_theme()This is convenient! I can now use the yan_holtz_theme() on any chart to have an uniformity without repeating myself too much.
You can also set the theme at the beginning of your Quarto doc and stop worrying about style for the whole study.
theme_set(yan_holtz_theme())
6 Quarto Tips
More Quarto tips
The level of customization possible in a Quarto document is practically limitless. 🙃
In the previous lesson, I endeavored to distill the most essential concepts for you. However, enumerating all the possibilities would likely result in a rather tedious course.
Instead, armed with a solid Quarto foundation, I recommend perusing the compilation of Quarto tips and tricks I maintain.
I’m confident you’ll discover several valuable insights that you’ll be eager to incorporate into your next report!
6.1 Centering figure
Another alternative
6.2 Plotly in Quarto Trick
plotly is such an amazing package. With the ggplotly function you can make your ggplot interactive in just 1 line of code!
6.3 ggiraph
6.4 ggiraph is one of the most powerful R package when it comes to interactive data visualization.
One feature I really like is the ability to connect 2 charts together.
Since quarto supports interactive outputs, let’s make the most of it! In the example below, you can hover over a circle on the map to highlight a line on the chart (and reciprocally)!
6.5 Callout
Use callouts to draw attention to important complementary content without interupting the document flow.
5 types of callout exist out of the box, and they are really easy to use. Here is an example with a note callout: